package com.examples.AppoointmentManager;



import java.util.ArrayList;


import com.examples.Model.AppoinModel;
import com.examples.database.AppoinAdapter;


import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;


public class Display extends ListActivity {
	
    private static final int ADD_CONTACT = 0;
	AlertDialog.Builder alert;
    AppoinAdapter ga;
    ListView lv;
    ArrayList<AppoinModel> groupsList;
    private Context ctx;
    
    int notebookid;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display); 
        
       
        ctx=this;
        ga=new AppoinAdapter(getApplicationContext());
		lv=getListView();
		lv.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				final int position=arg2;
				alert=new AlertDialog.Builder(ctx);
				alert.setTitle("Delete Group");
		    	alert.setMessage("Confrim delete group "+groupsList.get(arg2).getAgenda());
		    	alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					
						deleteGroup(position);
					}
				});
		    	alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {	
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				});
		    	alert.create().show();
				return true;
			}
		});
		refreshList();  
    }
	
	
	
	
	
    
   
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		/*refreshList();
		int groupId=groupsList.get(position).getId();
		String groupName=groupsList.get(position).getName();
		Intent i=new Intent(getApplicationContext(),GroupOperations.class);
		i.putExtra("grpId", groupId);
		i.putExtra("grpName", groupName);
		startActivityForResult(i, ADD_CONTACT);*/
		
		String groupName=groupsList.get(position).getAgenda();
		
		alert=new AlertDialog.Builder(ctx);
		alert.setTitle("View Content");
    	alert.setMessage(groupName);
    	alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
    	
    	alert.create().show();
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		refreshList();
	}
	protected void deleteGroup(Integer position){
		Integer groupId=position;
		Log.d("posssssssss",groupId.toString());
		Log.d("posssssssss",position.toString());
		ga.open();
		//String item = (String) getListAdapter().getItem(position);
		ga.deleteGroup(position);
		ga.close();
		refreshList();
	}
	private void refreshList(){
		ga.open();
		groupsList=ga.fetchGroups();
		lv.setAdapter(new LIstAdapter(getApplicationContext(),groupsList));
		ga.close();
	}
}